<template>
  <div>
    <el-dialog
      :title="editUserForm.updateType == 'zx'?'个人中心':'用户编辑'"
      :visible.sync="editUserVisible"
      width="40%"
      :show-close="false"
      :close-on-click-modal="false">
      <el-form ref="editUserForm" :model="editUserForm" :rules="editUserForm_rules" label-width="110px">
        <el-form-item label="id" prop="user_id">
          <el-input v-model="editUserForm.user_id" size="small" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="user_name">
          <el-input v-model="editUserForm.user_name" size="small" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="user_nick_name">
          <el-input v-model="editUserForm.user_nick_name" size="small" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="user_email">
          <el-input v-model="editUserForm.user_email" size="small" placeholder="该邮箱只能绑定一个账号" style="width: 80%" :disabled="isEmail"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="user_sex">
          <el-select v-model="editUserForm.user_sex" style="width: 80%;">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="closeEditUser">取 消</el-button>
        <el-button v-if="editUserForm.updateType == 'zx'" size="mini" type="info" @click="updatePwd">修改密码</el-button>
      <el-button size="mini" type="primary" @click="editUserInfo">确 定</el-button>
    </span>
    </el-dialog>
    <update-user-pwd-dialog v-if="isShowUpdatePwdDialog" :isShowUpdatePwdDialog="isShowUpdatePwdDialog" :updatePwdData="updatePwdData" @updateUserPwdClose="closeDialog"></update-user-pwd-dialog>
  </div>
</template>
<script>
  import {getAllUserByGroupId,updateUserByUserId,updateUserPasswordById,findUserByCondition,addUser} from '@/api/user'
  import {getAllGroup} from '@/api/permission'
  import updateUserPwdDialog from './updatepawword.vue'
  export default{
    components:{
      updateUserPwdDialog
    },
    props:["isShowEditDialog","userData"],
    data(){
      return {
        editUserVisible:this.isShowEditDialog,
        editUserForm:this.userData,

        isEmail:this.userData.updateType == "zx"?false:true,

        editUserForm_rules:{
          user_name:[{ required: true, message: '不能为空', trigger: 'change' },{ min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }],
          user_email:[{ required: true, message: '不能为空', trigger: 'change' }, {type: 'email',message: '请输入正确的邮箱地址',trigger: ['blur', 'change']}],
        },
        groupList:[],
        updatePwdData:{},
        isShowUpdatePwdDialog:false
      }
    },

    mounted(){

    },

    methods:{
      //关闭弹出框
      closeEditUser(){
        this.editUserVisible = false;
        if(this.editUserForm.updateType == "zx"){
          this.$emit("editUserInfoClose");
        }else{
          this.$emit("editUserClose","edit");
        }
      },

      updatePwd(){
        this.isShowUpdatePwdDialog = true;
      },

      closeDialog(){
          this.isShowUpdatePwdDialog = false;
      },

      //提交数据进添加
      editUserInfo(){
        this.$refs["editUserForm"].validate((valid) =>{
            if(valid){
                //调用后台接口进行数据修改
              updateUserByUserId(this.editUserForm).then(res=>{
                  if(res.code == 200){
                    this.$message({
                      message: '修改成功',
                      type: 'success'
                    });
                    this.closeEditUser();
                  }
              });
            }
        });
      },
    }
  }
</script>
